<template>
  <div>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="70px"
    >
      <el-form-item label="账号" prop="name">
        <el-input
          type="text"
          v-model="ruleForm.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <div class="options">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >注册</el-button
        >

        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-form>
    <p>
      <span class="quickLogin" @click="quickLogin">快速登入 > </span>
    </p>
  </div>
</template>

<script>
import { register } from "@/utils/api";
export default {
  data() {
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        if (this.ruleForm.name !== "") {
          this.$refs.ruleForm.validateField("name");
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        name: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        name: [{ validator: validateName, trigger: "blur" }],
      },
    };
  },
  methods: {
    async submitForm() {
      if (this.ruleForm.name == "" || this.ruleForm.pass == "") {
        this.$message.error("表单不能为空！！");
        return;
      }
      console.log(this.ruleForm.name);
      try {
        await register({
          username: this.ruleForm.name,
          password: this.ruleForm.pass,
        });
        this.$message.success("登入成功！！");
        this.$router.push("/login");
      } catch (err) {
        this.$message.error("注册失败，用户名已经被占用！！");
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    quickLogin() {
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped>
::v-deep(.el-input) {
  width: 300px;
}
.options {
  display: flex;
  justify-content: center;
  align-items: center;
}
.quickLogin {
  float: right;
  font-size: 12px;
  cursor: pointer;
  color: #409eff;
}
</style>